package link;

public class MyLinkedList {

    private  LinkedNode dummyHead ;
    private  int size ;
    MyLinkedList(){
        dummyHead = new LinkedNode(0);
        size = 0;
    }

    public  int get(int index){
        if(index>size||index< 0){
            return  -1;
        }
        LinkedNode cur = dummyHead.next;
        while((index--)>1){
            cur = cur.next;
        }
        System.out.println("get 后节点值:"+dummyHead+";value:"+cur.value);
        return cur.value;
    }

    public  void addHead(int val){
        LinkedNode newNode = new LinkedNode(val);
        newNode.next = dummyHead.next;
        dummyHead.next = newNode;
        size++;
        System.out.println("addHead 后节点值:"+dummyHead+";size:"+size);
    }

    public  void addTail(int val){
        LinkedNode newNode = new LinkedNode(val);
        LinkedNode cur = dummyHead;
        while(cur.next!=null){
            cur = cur.next;
        }
        cur.next = newNode;
        size++;
        System.out.println("addTail 后节点值:"+dummyHead+";size:"+size);
    }

    public  void addAtIndex(int index,int val){
        if(size < index){
            return ;
        }
        LinkedNode newNode = new LinkedNode(val);
        LinkedNode cur = dummyHead;
        while(index-->0){
            cur = cur.next;
        }
        newNode.next = cur.next;
        cur.next = newNode;
        size ++;
        System.out.println("addAtIndex 后节点值:"+dummyHead+";size:"+size);
    }

    public  void delete(int index){
        if(index>size){
            return;
        }
        LinkedNode cur = dummyHead;
        while(index-->1){
            cur = cur.next;
        }
        cur.next = cur.next.next;
        size -- ;
        System.out.println("delete 后节点值:"+dummyHead+";size:"+size);
    }

    public  void printLinkedList(){
        LinkedNode cur = dummyHead;
        while(cur.next!=null){
            System.out.println("当前节点:"+cur.next.value);
            cur = cur.next;
        }
    }



    public static void main(String[] args) {
        MyLinkedList mll = new MyLinkedList();
        mll.addTail(1);
        mll.get(1);
      /*  mll.addTail(3);
        mll.addTail(2);*/
        //mll.delete(3);
        //mll.addAtIndex(4,4);
        //mll.delete(2);
        //mll.addHead(2);
        /*
        mll.addHead(3);
        mll.printLinkedList();*/



    }

}
class LinkedNode{
    public int value ;
    public LinkedNode next;
    public LinkedNode(int data){
        this.value = data;
    }
    @Override
    public String toString() {
        return "Node{" + "value=" + value + ", next=" + next + '}';
    }
}
